package com.seedwill.common.view;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PageView implements IPageView, Serializable {
	
	public Map context;
	
	private int pageNavStep = 10;
	/**
	 * 默认分页容量
	 */
	public static int DEFAULT_PAGE_SIZE = 20;
	
	/**
	 * 默认开始页号
	 */
	private static int DEFAULT_START_PAGE = 1;

	public static PageView EMPTY_PAGE(){
		return new PageView();
	}
	
	private int startPage;
	
	private int endPage;
	/**
	 * 每页的记录数
	 */
	public int pageSize = DEFAULT_PAGE_SIZE;

	/**
	 * 开始记录号 从0开始
	 */
	public int start;
    /**
     * 结束记录号
     */
    public int end;
    /**
     * 显示在页面开始记录号，从1开始
     */
    public int viewStart;

    /**
     * 显示在页面结束记录号
     */
    public int viewEnd;
	/**
	 * 当前页中存放的记录,类型一般为List
	 */
	public List list;

	/**
	 * 总记录数
	 */
	public int totalCount;

	/**
	 * 构造方法，只构造空页.
	 */
	public PageView() {
		this(0, 0, DEFAULT_PAGE_SIZE, new ArrayList(DEFAULT_PAGE_SIZE));
	}

	/**
	 * 默认构造方法.
	 *
	 * @param start	 本页数据在数据库中的起始位置
	 * @param totalSize 数据库中总记录条数
	 * @param pageSize  本页容量
	 * @param data	  本页包含的数据
	 */
	public PageView(int start, int totalSize, int pageSize, List data) {
		this.pageSize = pageSize;
		this.start = start;
		this.totalCount = totalSize;
		this.list = data;
		context = new HashMap(32, 0.75f);
	}

	/**
	 * 取总记录数.
	 */
	public int getTotal() {
		return this.totalCount;
	}

	/**
	 * 取总页数.
	 */
	public int getTotalPage() {
		if (0 == totalCount % pageSize){
			return totalCount / pageSize;
		}else{
			return totalCount / pageSize + 1;
		}
	}

	/**
	 * 取每页数据容量.
	 */
	public int getPageSize() {
		return pageSize;
	}

	/**
	 * 取当前页中的记录.
	 */
	public List getList() {
		return list;
	}

	/**
	 * 取该页当前页码,页码从1开始.
	 */
	public int getCurrentPageNo() {
		return currentPageNumber;
		//return start / pageSize + 1;
	}

	/**
	 * 该页是否有下一页.
	 */
	public boolean hasNextPage() {
		return this.hasNext;
		//return this.getCurrentPageNo() < this.getTotalPage() - 1;
	}

	/**
	 * 该页是否有上一页.
	 */
	public boolean hasPreviousPage() {
		return this.hasPrevious;
	}

	public int getStartOfPage(int pageNo) {
		return getStartOfPage(pageNo, DEFAULT_PAGE_SIZE);
	}

	public int getStartOfPage(int pageNo, int pageSize) {
		if(pageNo<=0){ pageNo = 1;}
		return (pageNo - 1) * pageSize;
	}
	
	public int getDefaultPageSize() {
		return DEFAULT_PAGE_SIZE;
	}

	public int getDefaultStartPageNo() {
		return  DEFAULT_START_PAGE;
	}
 
	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public void setList(List list) {
		this.list = list;
	}
	
	/**
	 * 当前是第几页
	 */
	public int currentPageNumber;
	
    /**
     * 准备分页数据
	 * @param num
	 * @param size
	 * @param total    total
	 * 
	 */
    public void prepare(int num, int size, int total) {
        this.pageSize = size;
        this.totalCount = total;
    	//return start / pageSize + 1;
        this.currentPageNumber = num;
        
        if (0 == total){
            currentPageNumber = 0;
        }
        else{
            autoCalculate();
        }
    }

    /**
     * 一共有多少页
     */
    private int totalPage;
    /**
     * 是否有上一页的开关
     */
    public boolean hasPrevious;

    /**
     * 上一页的页码
     */
    public int previousPageNumber;
    /**
     * 最后页的页码
     */
    public int lastPageNumber;

    /**
     * 是否有下一页的开关
     */
    public boolean hasNext;

    /**
     * 下一页的页码
     */
    public int nextPageNumber;
    /**
     * 自动计算，根据当前页、页大小、总行数计算出其它属性的值
     */
    public void autoCalculate() {
    	if(0 == pageSize){
    		pageSize = DEFAULT_PAGE_SIZE;
    	}
        start = (currentPageNumber - 1) * pageSize;
        end = start + pageSize - 1;
        if (end > totalCount) {
            end = totalCount;
        }
        viewStart = start + 1;
        viewEnd = end + 1;
        totalPage = (totalCount + pageSize - 1) / pageSize;
        if (1 == currentPageNumber) {
            hasPrevious = false;
            previousPageNumber = 1;
        } else {
            hasPrevious = true;
            previousPageNumber = currentPageNumber - 1;
        }
        if (currentPageNumber == totalPage) {
            hasNext = false;
            nextPageNumber = totalPage;
        } else {
            hasNext = true;
            nextPageNumber = currentPageNumber + 1;
        }
        if(totalPage>0){
        	lastPageNumber = totalPage;
        }else{
        	totalPage = 0;
        }
        this.startPage = this.currentPageNumber-1;
        if(startPage<1){
        	startPage = 1;
        }
        this.endPage = this.startPage + this.pageNavStep;
        if(this.endPage>totalPage){
        	this.endPage = this.totalPage;
        }
    }

	public int getViewStart() {
		return viewStart;
	}

	public void setViewStart(int viewStart) {
		this.viewStart = viewStart;
	}

	public int getViewEnd() {
		return viewEnd;
	}

	public void setViewEnd(int viewEnd) {
		this.viewEnd = viewEnd;
	}

	public boolean isHasPrevious() {
		return hasPrevious;
	}

	public void setHasPrevious(boolean hasPrevious) {
		this.hasPrevious = hasPrevious;
	}

	public int getPreviousPageNumber() {
		return previousPageNumber;
	}

	public void setPreviousPageNumber(int previousPageNumber) {
		this.previousPageNumber = previousPageNumber;
	}

	public boolean isHasNext() {
		return hasNext;
	}

	public void setHasNext(boolean hasNext) {
		this.hasNext = hasNext;
	}

	public int getNextPageNumber() {
		return nextPageNumber;
	}

	public void setNextPageNumber(int nextPageNumber) {
		this.nextPageNumber = nextPageNumber;
	}

	public int getLastPageNumber() {
		return lastPageNumber;
	}

	public void setLastPageNumber(int lastPageNumber) {
		this.lastPageNumber = lastPageNumber;
	}

	public int getStartPage() {
		return startPage;
	}

	public void setStartPage(int startPage) {
		this.startPage = startPage;
	}

	public int getEndPage() {
		return endPage;
	}

	public void setEndPage(int endPage) {
		this.endPage = endPage;
	}

	public int getEnd() {
		return end;
	}

	public void setEnd(int end) {
		this.end = end;
	}

	public int getCurrentPageNumber() {
		return currentPageNumber;
	}

	public void setCurrentPageNumber(int currentPageNumber) {
		this.currentPageNumber = currentPageNumber;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public int getPageNavStep() {
		return pageNavStep;
	}

	public void setPageNavStep(int pageNavStep) {
		this.pageNavStep = pageNavStep;
	}
}
